package dq.study.designmodel.iteator;

import dq.study.designmodel.iteator.Collection;
/**
 * @author dongqun
 * @Description:
 * @date 2019/3/13 22:00
 */
public class ArrayList<E> implements Collection<E> {
    Object[] array = new Object[10];
    int index = 0;

    @Override
    public void add(E e) {
        if (index==array.length){
            Object[] newArray = new Object[array.length*2];
            System.arraycopy(array,0,newArray,0,array.length);
            array = newArray;
        }
        array[index] = e;
        index++;
    }

    @Override
    public int size() {
        return index;
    }

    @Override
    public Iterator iterator() {
        return new ArrayListIterator();
    }

    private class ArrayListIterator implements Iterator<E>{

        private int count;

        @Override
        public boolean hasNext() {
            if (count<index) return true;
            return false;
        }

        @Override
        public E next() {
            E e = (E) array[count];
            count++;
            return e;
        }
    }
}
